﻿function SelectSource(that) {
  if (!$(that).hasClass('SelectedSource')) {
    $('.DeleteButton').hide();
    $(that).find('.DeleteButton').show();
    $('.SelectedSource').removeClass('SelectedSource');
    $(that).addClass('SelectedSource');

    var SourcesToReed = new Array();

    if ($(that).find('.FeedSourceLink').length > 0) {
      SourcesToReed.push({
        Title: $(that).find('.FeedSourceTitle').val().trim(),
        Link: $(that).find('.FeedSourceLink').val().trim(),
      });
    }
    else {
      var Sources = $('li.Source');
      for (var i = 0; i < Sources.length; ++i) {
        SourcesToReed.push({
          Title: $(Sources[i]).find('.FeedSourceTitle').val().trim(),
          Link: $(Sources[i]).find('.FeedSourceLink').val().trim(),
        });
      }
    }

    $('#RSSReaderFeedsContainer').empty();
    $('#FeedLoadingImage').show();
    $.ajax({
      contentType: 'application/json; charset=utf-8',
      type: 'POST',
      url: '/Home/GetFeedArticle',
      data: JSON.stringify(SourcesToReed),
      success: function (PartialView) {
        $('#FeedLoadingImage').hide();
        $('#RSSReaderFeedsContainer').append(PartialView);
      },
      failure: function (response) {
        $('#result').html(response);
      }
    });
  }
}

function MenuMouseOver(that) {
  $('.DeleteButton').hide();
  $(that).find('.DeleteButton').show();
  $('.SelectedSource .DeleteButton').show();
}

function MenuMouseOut(that) {
  if (!$(that).hasClass('SelectedSource'))
    $(that).find('.DeleteButton').hide();
}

function ToggleArticle(that, url) {
  var ArticleHeaderDiv = $(that).closest('.FeedHeaderContainer');
  var WholeArticleDiv = $(that).closest('.FeedArticle');
  ArticleHeaderDiv.toggleClass('BottomBorder');
  ArticleHeaderDiv.find('.FeedFavoriteButton').toggleClass('ButtomLeftRadius');
  WholeArticleDiv.find('.FeedContent').toggle();
  if (!WholeArticleDiv.hasClass('FeedArticleIsRed')) {
    SetArticleInfo(that);
  }
}

function ToggleFavorite(that) {
  $(that).toggleClass('FeedIsFavorite');
  SetArticleInfo(that);
}

function AddSource() {
  if ($('#NewSourceLink').val().trim().length > 0) {
    $('#SourceErrorMessage').remove();
    $('#AddSourceButton').hide();
    $('#SourceLoadingImage').show();
    $.post('/Home/AddSource/', {
      source: $('#NewSourceLink').val().trim(),
    }, function (PartialView) {
      $('#SourceLoadingImage').hide();
      $('#AddSourceButton').show();
      if ($(PartialView).filter("#SourceErrorMessage").length == 0) {
        $('#SourceList').append(PartialView);
        $('#NewSourceLink').val('');
      }
      else
        $('#SourceMessageDisplay').append(PartialView);
    }).fail(function () {
      $('#SourceLoadingImage').hide();
      $('#AddSourceButton').show();
      alert('Nope');
    });
  }
}

function UnsubscribeFrom(link, that, event) {
  event.stopPropagation();
  if (confirm('Etes-vous sur de vouloir supprimer cet abonnement?'))
    $.post('/Home/Unsubscribe/', {
      sourceLink: link
    }, function () {
      $('.SelectedSource').removeClass('SelectedSource');
      $('#RSSReaderFeedsContainer').empty();
      $(that).closest('li').remove();
    });
}

function SetArticleInfo(that, event) {
  var WholeArticleDiv = $(that).closest('.FeedArticle');
  WholeArticleDiv.addClass('FeedArticleIsRed');
  if (event != undefined)
    event.stopPropagation();
  var SourceLink = WholeArticleDiv.find('.FeedArticleSourceLink').val();
  var ArticleLink = WholeArticleDiv.find('.FeedArticleLink').attr('href');
  var IsFavorite = WholeArticleDiv.find('.FeedIsFavorite').length > 0;

  $.post('/Home/UpdateArticleStatus/', {
    sourceUrl: SourceLink,
    articleUrl: ArticleLink,
    IsFavorite: IsFavorite,
  }, function (data) {
  });
}

function GetUserFavorites(that) {
  if (!$(that).hasClass('SelectedSource')) {
    $('.SelectedSource').removeClass('SelectedSource');
    $(that).addClass('SelectedSource');
    $('#RSSReaderFeedsContainer').empty();
    $('#FeedLoadingImage').show();

    $.post('/Home/GetFavoritesArticles/', {}, function (PartialView) {
      $('#FeedLoadingImage').hide();
      $('#RSSReaderFeedsContainer').append(PartialView);
    });
  }
}

function Debug_DisplayObjectContent(obj) {
  var res = '';

  for (var member in obj) {
    res += 'obj[' + member + '] = (' + obj[member] + ')\n';
  }
  return res;
}